﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for AuthenticationBLL
/// </summary>
public class AuthenticationBLL
{
    UserDAL userDAL;

	public AuthenticationBLL()
	{
        try
        {
            /*initilaize db variables*/
            userDAL = new UserDAL();
        }
        catch (Exception ex)
        {
            throw ex;
        }
	}

    public DataResponse<User> GetUser(string username)
    {
        return userDAL.GetUser(username);
    }

    public DataResponse<bool?> IsUserLoginValid(string username, string password)
    {
        DataResponse<bool?> response = new DataResponse<bool?>();

        DataResponse<User> userResponse = GetUser(username);

        if (userResponse != null)
        {
            if (string.IsNullOrEmpty(userResponse.ErrorCode))
            {
                if (string.Equals(userResponse.Result.Username, username, StringComparison.InvariantCultureIgnoreCase) && string.Equals(userResponse.Result.Password, password, StringComparison.InvariantCultureIgnoreCase))
                {
                    response.Result = true;
                }
                else
                {
                    response.Result = false;
                }
            }
            else
            {
                response.ErrorCode = userResponse.ErrorCode;
                response.ErrorMsg = userResponse.ErrorMsg;
            }
        }
        else
        {
            response.Result = false;
        }

        return response;
    }
}